package com.uyaer.wheel.display.scene
{
	import com.uyaer.wheel.common.Common;
	import com.uyaer.wheel.common.Config;
	import com.uyaer.wheel.common.Platform;
	import com.uyaer.wheel.res.ResManager;
	
	import starling.core.Starling;
	import starling.display.Image;
	import starling.display.Quad;
	import starling.display.Sprite;
	import starling.events.Touch;
	import starling.events.TouchEvent;
	import starling.events.TouchPhase;
	import starling.text.TextField;
	import starling.textures.Texture;
	import starling.utils.HAlign;
	import starling.utils.VAlign;
	
	public class Guide extends Sprite
	{

		private var mask:Quad;
		public function Guide()
		{
			layout();
		}
		
		private function onTouch(e:TouchEvent):void{
			var touch:Touch = e.getTouch(this);
			if(touch && touch.phase == TouchPhase.ENDED){
				stop();
			}
		}
		
		private function layout():void{
			mask = new Quad(Common.W,Common.H,0x0ffffff);
			addChild(mask);
			
			var fontSize:Number = 10+32*Common.S;
			var titleTF:TextField = new TextField(Common.W*0.8,fontSize+10,"游戏说明");
			titleTF.color = 0x0ba913;
			titleTF.fontSize = fontSize;
			titleTF.x = Common.W*0.1;
			titleTF.y = Common.H*0.075;
			titleTF.touchable = false;
			addChild(titleTF);
			
			var desc:String = "这是一场逃亡的游戏，你行吗？\n" +
				"游戏操作很简单，晃动手机控制方向。\n" +
				"逃亡过程中努力收集星星吧，在进度条消失之前收集到下一颗星星，积分会猛涨哦。\n" +
				"千万不要让小球消失，否则只能很遗憾的说：“game over!”";
			if(Config.platform == Platform.WEB){
				desc = "这是一场逃亡的游戏，你行吗？\n" +
					"游戏操作很简单，键盘上下左右控制方向。\n" +
					"逃亡过程中努力收集星星吧，在进度条消失之前收集到下一颗星星，积分会猛涨哦。\n" +
					"千万不要让小球消失，否则只能很遗憾的说：“game over!”";
			}
			
			var infoTitle:TextField = new TextField(Common.W*0.9,Common.H*0.65-titleTF.bounds.bottom,desc);
			infoTitle.fontSize = 10+20*Common.S;
			infoTitle.vAlign = VAlign.CENTER;
			infoTitle.hAlign = HAlign.LEFT;
			infoTitle.color = 0x0ba913;
			infoTitle.x = Common.W*0.05;
			infoTitle.y = titleTF.bounds.bottom;
			infoTitle.touchable = false;
			addChild(infoTitle);
			
			var guideTexture:Texture = null;
			if(Config.platform == Platform.MOBILE){
				guideTexture = ResManager.me.getTexture("menu_guide_phone");
			}else if(Config.platform == Platform.WEB){
				guideTexture = ResManager.me.getTexture("menu_guide_key");
			}
			var phone:Image = new Image(guideTexture);
			phone.pivotX = phone.width>>1;
			phone.x = Common.W>>1;
			phone.y = Common.H*0.65;
			phone.scaleX = phone.scaleY = Common.S;
			phone.touchable = false;
			addChild(phone);
		}
		
		public function start():void{
			this.x = -Common.W;
			Starling.juggler.tween(this,0.75,{"x":0,"onComplete":function():void{
				mask.addEventListener(TouchEvent.TOUCH,onTouch);
			}});
		}
		
		public function stop():void{
			var that:Guide = this;
			mask.removeEventListener(TouchEvent.TOUCH,onTouch);
			Starling.juggler.tween(this,0.75,{"x":Common.H,"onComplete":function():void{
				that.removeFromParent();
			}});
		}
	}
}